<template>
    <BasePage>
        <el-form slot="form" :inline="true" size="medium" @submit.native.prevent>
            <el-form-item label="模型名称">
                <el-input v-model.trim="form.modelName" clearable placeholder="请输入" />
            </el-form-item>
            <el-form-item label="路面类型">
                <el-select v-model="form.pavementType" placeholder="请选择" clearable style="width: 100%">
                    <el-option v-for="d in RoadTypeOptions" :key="d.value" :label="d.label" :value="d.label" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <BaseButton v-permission="{ btnCode: 'dlyh_c53_d245_2' }" @click="handleFilter">查询</BaseButton>
                <BaseButton v-permission="{ btnCode: 'dlyh_c53_d245_2' }" type="second" @click="resetConditions">重置</BaseButton>
            </el-form-item>
        </el-form>

        <div slot="button">
            <BaseButton v-permission="{ btnCode: 'dlyh_c53_d240_2' }" @click="handleAdd">新增</BaseButton>
            <BaseButton v-permission="{ btnCode: 'dlyh_c53_d241_2' }" style="margin-right: 8px" :disabled="selectionList.length === 0" type="second" @click="handleDelete">
                删除
            </BaseButton>
        </div>

        <!--数据列表区-->
        <BaseTable
            slot="table"
            v-loading="listLoading"
            :columns="columns"
            :data="list"
            row-key="id"
            height="calc(100% - 73px)"
            highlight-current-row
            @selection-change="handleSelectionChange"
        >
            <template #index="{ scope: { $index } }">
                <span>{{ $index + 1 + (page.pageNo - 1) * page.pageSize }}</span>
            </template>
            <template #formula="{ scope: { row } }">
                <img :src="row.formulaPhoto" alt="模型公式" style="height: 32px" />
            </template>
            <template #handle="{ scope: { row } }">
                <BaseButton v-permission="{ btnCode: 'dlyh_c53_d242_2' }" type="text" @click="handleEdit(row.id)">修改</BaseButton>
                <BaseButton v-permission="{ btnCode: 'dlyh_c53_d243_2' }" type="text" @click="handleMaintain(row)">模型参数维护</BaseButton>
                <BaseButton v-permission="{ btnCode: 'dlyh_c53_d244_2' }" type="text" @click="handleUse(row.id)">应用情况</BaseButton>
            </template>
        </BaseTable>
        <!--分页组件-->
        <Pagination
            slot="pagination"
            :current-page.sync="page.pageNo"
            :page-size.sync="page.pageSize"
            :total="page.total"
            @current-change="getList"
            @size-change="getList"
        />
        <SaveOrUpdate ref="dialog" @success="handleFilter"></SaveOrUpdate>
        <UseDialog slot="other" ref="useDialog"></UseDialog>
    </BasePage>
</template>

<script>
import BasePage from '@/layouts/BasePage'
import reconfirm from '@/utils/reconfirm'
import { PerformancePredictionModel } from '@/models'
import KeepAliveHandler from '@/mixins/keepAliveHandler'
import { RoadTypeOptions } from '@/views/technical-evaluation/constants'
import SaveOrUpdate from './SaveOrUpdate'
import UseDialog from './UseDialog'

export default {
    name: 'PerformancePredictionList',
    components: {
        BasePage,
        SaveOrUpdate,
        UseDialog
    },
    mixins: [
        KeepAliveHandler({
            defaultCondition: {
                modelName: '',
                pavementType: ''
            },
            conditionObjKey: 'form'
        })
    ],
    data() {
        return {
            RoadTypeOptions,
            downloadLoading: false,
            templateLoading: false,
            model: new PerformancePredictionModel(),
            form: {
                modelName: '',
                pavementType: ''
            },
            listLoading: true,
            list: [],
            page: {
                pageNo: 1,
                pageSize: 10,
                total: 0
            },
            columns: Object.freeze([
                {
                    attrs: {
                        type: 'selection'
                    }
                },
                {
                    slot: 'index',
                    attrs: {
                        type: 'index',
                        label: '序号',
                        width: '70'
                    }
                },
                {
                    attrs: {
                        prop: 'modelName',
                        label: '模型名称',
                        'show-overflow-tooltip': true,
                        width: '170'
                    }
                },
                {
                    attrs: {
                        prop: 'modelFormulaName',
                        label: '模型公式名称',
                        'show-overflow-tooltip': true,
                        width: '150'
                    }
                },
                {
                    slot: 'formula',
                    attrs: {
                        prop: 'formulaPhoto',
                        label: '模型公式',
                        'show-overflow-tooltip': true,
                        width: '200'
                    }
                },
                {
                    attrs: {
                        prop: 'pavementType',
                        label: '路面类型',
                        'show-overflow-tooltip': true,
                        width: '130'
                    }
                },
                {
                    attrs: {
                        prop: 'pavementStructure',
                        label: '路面结构',
                        'show-overflow-tooltip': true,
                        'min-width': '150'
                    }
                },
                {
                    slot: 'handle',
                    attrs: {
                        label: '操作',
                        width: '260',
                        fixed: 'right'
                    }
                }
            ]),
            selectionList: []
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            this.listLoading = true
            this.model
                .getList(this.form, this.page)
                .then((res) => {
                    this.list = res.list.map((item) => {
                        return {
                            ...item,
                            formulaPhoto: window.api_source.url + item.formulaPhoto
                        }
                    })
                    this.page.total = res.totalCount
                })
                .finally(() => {
                    this.listLoading = false
                })
        },
        // 查询
        handleFilter() {
            this.page.pageNo = 1
            this.getList()
        },
        handleDetail(id, code) {
            this.$router.push({ name: 'TunnelStationDetail', query: { id, code, type: 'detail' } })
        },
        handleUse(id) {
            this.$refs['useDialog'].open(id)
        },
        handleAdd() {
            this.$refs['dialog'].open({ type: 'add' })
        },
        handleEdit(id) {
            this.$refs['dialog'].open({ id, type: 'edit' })
        },
        handleDelete() {
            reconfirm({
                text: '是否删除当前数据',
                callback: () => {
                    const ids = this.selectionList.map((item) => item.id)
                    return this.model.delete({ ids }).then(() => {
                        this.getList(true)
                        this.$message.success('删除成功!')
                    })
                }
            })
        },
        handleSelectionChange(value) {
            this.selectionList = value
        },
        handleMaintain(row) {
            this.$router.push({
                name: 'ParameterMaintenance',
                query: {
                    modelName: row.modelName,
                    modelFormulaName: row.modelFormulaName,
                    pavementType: row.pavementType,
                    id: row.id,
                    modelFormula: row.modelFormula,
                    url: row.formulaPhoto
                }
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.filter-stake {
    width: 203px;
    ::v-deep {
        .el-input__inner,
        .el-input {
            min-width: auto !important;
            line-height: 1px !important;
        }
        .el-input-group__append,
        .el-input-group__prepend {
            padding: 0 8px;
            color: #44444f;
        }
    }
    .start-stake {
        position: relative;
        z-index: 1;
        margin-right: -1px;
        ::v-deep {
            .el-input__inner,
            .el-input {
                border-radius: 0;
            }
        }
    }
    .end-stake {
        ::v-deep {
            .el-input-group__append,
            .el-input-group__prepend {
                border-radius: 0;
            }
        }
    }
}
</style>
